<?php
/**
 * 行政区插件
 * 
 * @author 天软农联
 */
namespace Admin\Controller;

use Common\Controller\AppframeController;

class ChinaCityController extends AppframeController {
    protected $chinaCity_model;
    public function _initialize() {
        parent::_initialize();
        $this->chinaCity_model = D("Common/ChinaCity");
    }
	// 获取省份信息
	public function getProvince() {
	    if (IS_AJAX) {
    		$pid = I ( 'pid' ); // 默认的省份id
    		$select = I ( 'select' );
    		$map ['level'] = 1;
    		$map ['upid'] = 0;
    		$list = $this->chinaCity_model->where($map)->order("id")->select();
    		
    		$data = " <option value =''>-省份-</option>";
    		foreach ( $list as $k => $vo ) {
    			$data .= "<option ";
    			if ($pid == $vo ['id']) {
    				$data .= " selected ";
    			} elseif (!empty ( $select ) && $select == $vo ['id']) {
    				$data .= " selected ";
    			}
    			$data .= " value ='" . $vo ['id'] . "'>" . $vo ['name'] . "</option>";
    		}
    		$this->ajaxReturn ( $data );
	    }
	}
	
	// 获取城市信息
	public function getCity() {
		if (IS_AJAX) {
			$select = I ( 'select' );
			$cid = I ( 'cid' ); // 默认的城市id
			$pid = I ( 'pid' ); // 传过来的省份id
			$map ['level'] = 2;
			$map ['upid'] = $pid;
			$list = $this->chinaCity_model->where($map)->order("id")->select();
			$data = " <option value =''>-城市-</option>";
			foreach ( $list as $k => $vo ) {
				$data .= "<option ";
				if ($cid == $vo ['id']) {
					$data .= " selected ";
				} elseif (!empty ( $select ) && $select == $vo ['id']) {
					$data .= " selected ";
				}
				$data .= " value ='" . $vo ['id'] . "'>" . $vo ['name'] . "</option>";
			}
			$this->ajaxReturn ( $data );
		}
	}
	
	// 获取区县市信息
	public function getDistrict() {
		if (IS_AJAX) {
			$select = I ( 'select' );
			$did = I ( 'did' ); // 默认的城市id
			$cid = I ( 'cid' ); // 传过来的城市id
			$map ['level'] = 3;
			$map ['upid'] = $cid;
			$list = $this->chinaCity_model->where($map)->order("id")->select();
			$data = " <option value =''>-州县-</option>";
			foreach ( $list as $k => $vo ) {
				$data .= "<option ";
				if ($did == $vo ['id']) {
					$data .= " selected ";
				} elseif (!empty ( $select ) && $select == $vo ['id']) {
					$data .= " selected ";
				}
				$data .= " value ='" . $vo ['id'] . "'>" . $vo ['name'] . "</option>";
			}
			$this->ajaxReturn ( $data );
		}
	}
	

	public function chinacity() {
		$this->display ();
	}
}